package beta;

import beta.Item;
import java.util.Enumeration;
import java.util.Vector;
import javax.microedition.lcdui.Graphics;


public class Form extends Screen
{
    private Vector itemList = new Vector();
    
    private int itemsHeight = -1;
    
    private int selectedIndex = -1;
    private int firstVisibleIndex = -1;
    private int verticalShift = 0;
    private int pickingPosition = 0;
    
    private boolean keyConsumed = true;
    private boolean invalidated = true;
    
    public Form(String title)
    {
        setTitle(title);
    }
    
    private void invalidate()
    {
        invalidated = true;
    }

    void showNotify()
    {
        if(itemList.size() > 0 && ((ItemWrapper)itemList.elementAt(0)).item.isFocusable() == true)
            selectedIndex = 0;
    }
    
    
    
    public void add(Item item)
    {
        itemList.addElement(new ItemWrapper(item));
        invalidate();
    }
    
    public void insert(Item item, int index)
    {
        itemList.insertElementAt(item, index);
        invalidate();
    }

    public void remove(Item item)
    {
        ItemWrapper w;
        for(int i = 0; i < itemList.size(); ++i)
        {
            w = (ItemWrapper)itemList.elementAt(i);
            if(w.item == item)
            {
                itemList.removeElementAt(i);
                invalidate();
                break;
            }
        }
    }
    
    public void remove(int index)
    {
        itemList.removeElementAt(index);
        invalidate();
    }
    
    public void removeAll()
    {
        itemList.removeAllElements();
        invalidate();
    }

    void onKeyPressed(int key)
    {
        System.out.println("key pressed: " + key + "......down(" + DOWN_KEY + ") ? " + (key == DOWN_KEY) + ", up("+ UP_KEY +") ? " + (key == UP_KEY));
        if(selectedIndex >= 0)
        {
            ItemWrapper w = (ItemWrapper)itemList.elementAt(selectedIndex);
            keyConsumed = w.item.onKeyPressed(key);
            if((key == DOWN_KEY || key == UP_KEY) && keyConsumed == false)
                    scrollForm(key);    // przewijamy formę jeśli trzeba
        }
        else if(key == DOWN_KEY || key == UP_KEY)
            scrollForm(key);    // przewijamy formę jeśli trzeba
    }

    void onKeyReleased(int key)
    {
        if(selectedIndex >= 0)
        {
            ItemWrapper w = (ItemWrapper)itemList.elementAt(selectedIndex);
            w.item.onKeyReleased(key);
        }
    }

    void onKeyRepeated(int key)
    {
        if(selectedIndex >= 0)
        {
            ItemWrapper w = (ItemWrapper)itemList.elementAt(selectedIndex);
            if(keyConsumed == true)
                w.item.onKeyRepeated(key);
            else if(key == DOWN_KEY || key == UP_KEY)
                    scrollForm(key);    // przewijamy formę jeśli trzeba
        }
        else if(key == DOWN_KEY || key == UP_KEY)
            scrollForm(key);    // przewijamy formę jeśli trzeba
    }

    private void scrollForm(int direction)
    {
        if(itemList.size() == 0)    // forma jest pusta nie ma czego przewijać
            return;
        if(firstVisibleIndex == -1)
            firstVisibleIndex = 0;
        if(invalidated)
        {
            doLayout();
            invalidated = false;
        }
        
        int height = ScreenManager.getScreenHeight(false);
        int halfHeight = height / 2;
        ItemWrapper w = (ItemWrapper)itemList.elementAt(firstVisibleIndex);
        
        if(direction == UP_KEY && pickingPosition > 0)
        {
            pickingPosition -= 20;
            if(pickingPosition < verticalShift + halfHeight)
            {
                verticalShift = pickingPosition - halfHeight;
                if(verticalShift < 0)
                    verticalShift = 0;
            }
            while(w.position > verticalShift)
                w = (ItemWrapper)itemList.elementAt(--firstVisibleIndex); 
        }
        else if(direction == DOWN_KEY && pickingPosition < itemsHeight)
        {
            pickingPosition += 20;
            if(pickingPosition > verticalShift + halfHeight)
            {
                verticalShift = pickingPosition - halfHeight;
                if(verticalShift + height > itemsHeight)
                    verticalShift = itemsHeight - height;
            }
            while(w.position + w.height < verticalShift)
                w = (ItemWrapper)itemList.elementAt(++firstVisibleIndex); 
        }
        int i = firstVisibleIndex;
        while(w.position + w.height < pickingPosition && i < itemList.size() - 1)
            w = (ItemWrapper)itemList.elementAt(++i);
        if(w.item.isFocusable())
            selectedIndex = i;
        else
            selectedIndex = -1;   

        System.out.println("byebye... height: " + height);
        ScreenManager.callRepaint();
    }

    void paint(Graphics g, int width, int height)
    {
        ItemWrapper w;
        Enumeration list = itemList.elements();
        int i = 0;
        Theme theme = ScreenManager.getCurrentTheme();
        if(invalidated)
        {
            doLayout();
            invalidated = false;
        }

        if(itemsHeight > height)
        {
            g.translate(width - theme.getScrollBarSpace(), 0);
            theme.drawVerticalScrollBar(g, height, pickingPosition, itemsHeight, height);
            width -= theme.getScrollBarSpace();
            g.translate(-width, 0);
        }
        
        System.out.println("printing....height: " + height);
        
        int shift;
        while(list.hasMoreElements())
        {
            w = (ItemWrapper)list.nextElement();
            shift = w.position - verticalShift;
            g.translate(0, shift);
            w.item.print(g, i++ == selectedIndex, width, w.height);
            g.translate(0, -shift);
        }
        g.translate(0, pickingPosition - verticalShift);
        g.fillRect(0, -2, width, 2);
        g.translate(0, -pickingPosition + verticalShift);
    }
    
    private void doLayout()
    {
        ItemWrapper w;
        Enumeration list = itemList.elements();
        int currentPosition = 0;
        int width = ScreenManager.getScreenWidth();
        while(list.hasMoreElements())
        {
            w = (ItemWrapper)list.nextElement();
            w.height = w.item.getPreferredHeight(width);
            w.position = currentPosition;
            currentPosition += w.height;
            
        }
        itemsHeight = currentPosition;
    }
    
    private class ItemWrapper
    {
        public Item item;
        public int position;
        public int height = -1;

        public ItemWrapper(Item newItem)
        {
            item = newItem;
        }
    }
}
